package cn.cqautotest.cqviehelper.adapters;

import android.annotation.SuppressLint;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.zhuangfei.timetable.model.Schedule;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.cqautotest.cqviehelper.R;
import cn.cqautotest.cqviehelper.config.Constants;
import cn.cqautotest.cqviehelper.utils.MyScheduleSupport;

@SuppressLint("NonConstantResourceId")
public class ItineraryAdapter extends RecyclerView.Adapter<ItineraryAdapter.InnerHolder> {

    private final List<Schedule> objects = new ArrayList<>();

    public void setObjects(List<Schedule> data) {
        objects.clear();
        objects.addAll(data);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_itinerary, parent, false);
        return new InnerHolder(itemView);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        Schedule schedule = objects.get(position);
        Map<String, Object> extras = schedule.getExtras();
        boolean haveCourse = (boolean) extras.getOrDefault(Constants.HAVE_COURSE, true);
        View llFestivalInfoContainer = holder.llFestivalInfoContainer;
        TextView tvNoCourseTips = holder.tvNoCourseTips;
        llFestivalInfoContainer.setVisibility(haveCourse ? View.VISIBLE : View.INVISIBLE);
        tvNoCourseTips.setVisibility(!haveCourse ? View.VISIBLE : View.INVISIBLE);
        if (tvNoCourseTips.getVisibility() == View.VISIBLE) {
            tvNoCourseTips.setText("当天没有课程哦~");
            return;
        }
        TextView tvFestival = holder.tvFestival;
        int startFestival = schedule.getStart();
        int endFestival = startFestival + schedule.getStep() - 1;
        // TODO: 2020/9/8 节次
        tvFestival.setText(String.format("第%s - %1s节", startFestival, endFestival));
        TextView tvCourseName = holder.tvCourseName;
        tvCourseName.setText(schedule.getName());
        TextView tvClassTime = holder.tvClassTime;
        // TODO: 2020/9/8 上课时间
        String[] festivalIntervalTime = MyScheduleSupport.getFestivalIntervalTime(startFestival, endFestival);
        tvClassTime.setText(String.format("%s - %1s", festivalIntervalTime[0], festivalIntervalTime[1]));
        TextView tvTeacherName = holder.tvTeacherName;
        tvTeacherName.setText(String.format("%s | %1s", schedule.getRoom(), schedule.getTeacher()));
        View itemView = holder.itemView;
        itemView.setOnLongClickListener(v -> {
            // TODO: 2020/11/15 没有课程，且设置了长按监听时才会触发
            if (tvNoCourseTips.getVisibility() != View.VISIBLE && mOnItemLongClickListener != null) {
                mOnItemLongClickListener.onItemLongClick(schedule);
            }
            return true;
        });
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        mOnItemLongClickListener = onItemLongClickListener;
    }

    private OnItemLongClickListener mOnItemLongClickListener = null;

    public interface OnItemLongClickListener {
        void onItemLongClick(Schedule schedule);
    }

    @Override
    public int getItemCount() {
        return objects.size();
    }

    public static class InnerHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.llFestivalInfoContainer)
        View llFestivalInfoContainer;
        @BindView(R.id.tvFestival)
        TextView tvFestival;
        @BindView(R.id.tvCourseName)
        TextView tvCourseName;
        @BindView(R.id.tvClassTime)
        TextView tvClassTime;
        @BindView(R.id.tvRoomAndTeacherName)
        TextView tvTeacherName;
        @BindView(R.id.tvNoCourseTips)
        TextView tvNoCourseTips;

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
